// pages/order_detail/index.js
const app     = getApp()
const request = require('../../utils/request.js');
const config = require('../../utils/config.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        order_id:0,
       order_detail:null
    },
    gorefund(e){
      wx.navigateTo({
        url: '/pages/refund/index?order_id='+this.data.order_id
      })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            order_id:options.order_id
        })
       
    },
    getDetail()
    {
        var that = this;
        var json = {};
       
       json.order_id=that.data.order_id
        request.requestLoading(
            "/api/order/orderDetail",
            json,
            'GET',
          ).then((res) => {
           
        that.setData({
            order_detail:res.data,
        })
           
      
          }).catch((err) => {
            console.error(err);
          });
    },
    cancelOrder(e){
        var json={},that=this;
        json.order_id=that.data.order_id
        request.requestLoading(
            "/api/order/cancelOrder",
            json,
            'POST',
          ).then((res) => {
            if(res.code==1){
                
                that.getDetail()
                getApp().globalData.is_need_refresh=true
               }else{
                   wx.showToast({
                     title: res.msg,
                     icon:'none'
                   })
               }
           
           
      
          }).catch((err) => {
            console.error(err);
          });
    },
    payOrder(e){
        var json={},that=this;
        json.order_id=that.data.order_id
        request.requestLoading(
            "/api/order/payNow",
            json,
            'POST',
          ).then((res) => {
            if(res.code==1){
                
                  if(res.data.is_pay==1){
                  
                    var wxconfig = res.data.wxconfig;
                    // 支付
                    wx.requestPayment({
                      timeStamp: wxconfig.timeStamp,
                      nonceStr: wxconfig.nonceStr,
                      package: wxconfig.package,
                      signType: wxconfig.signType,
                      paySign: wxconfig.paySign,
                      success() {
                       that.init_Data()
                       getApp().globalData.is_need_refresh=true
                      },
                      fail() {
                       wx.showToast({
                         title: '支付取消',
                         icon:'none'
                       })
                      }
                    })
                  }else{
                    that.setData({
                        buyPop:true
                    })
                  }
               }
           
           
      
          }).catch((err) => {
            console.error(err);
          });
    },
    gobenfits(){
      wx.navigateTo({
        url: '/pages/my_rights/index',
      })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getDetail()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.getDetail()
   
        setTimeout(function(){
          wx.stopPullDownRefresh();
        },500)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})